<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script src="stack.js"></script>
    <script>

        function dec2bin(decNumber) {
            var stack = new Stack()
            var remainder

            // 循环除法
            while (decNumber > 0) {
                remainder = decNumber % 2
                stack.push(remainder)
                // floor是向下取整
                decNumber = Math.floor(decNumber / 2)
            }

            console.log(stack.toString());

            // 从栈顶取值
            var binaryString = ''
            while(stack.size() > 0) {
                binaryString += stack.pop()
            }

            return binaryString
        }

        // 栈代码测试
        var stack = new Stack()
        stack.push(1)
        stack.push(2)
        // console.log(stack)
        // stack.size()

        // 转二进制
        var test = dec2bin(235)
        console.log(test);

    </script>
    
</body>
</html>